package bidsanity

class CustomerController {

    def index() {
        [customers: Customer.list(), customerInstanceTotal: Customer.count()]
    }

    def create() {
        [customerInstance: new Customer(params)]
    }
    
    def show(){
        def customerInstance = Customer.get(params.id)
        if (!customerInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'customer.label', default: 'Listing'), params.id])
            redirect(action: "index")
            return
        }
        [customer: customerInstance]
    }

    def delete(){
        def customerInstance = Customer.get(params.id)
        if (!customerInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'customer.label', default: 'Customer'), params.id])
            redirect(action: "index")
        }
        else
        {
            if(Bid.findAllByBidder(customerInstance).size() > 0)
            {
                flash.message = message(code: 'customer.bids.not.empty', args: [customerInstance.emailAddress])
                redirect(action: "show", id: customerInstance.id)
            }
            else if (Listing.findAllBySeller(customerInstance).size() > 0)
            {
                flash.message = message(code: 'customer.has.listing', args: [customerInstance.emailAddress])
                redirect(action: "show", id: customerInstance.id)
            }
            else
            {
                customerInstance.delete(failOnError: false)
                if (customerInstance.hasErrors())
                {
                    redirect(action: "index")
                }
                else
                {
                    flash.message = message(code: 'default.deleted.message', args: [message(code: 'customer.label', default: 'Customer'), customerInstance.id])
                    redirect(action: "index")
                }
            }

        }
    }    
    
    
    def save() {
        def customerInstance = new Customer(params)
        if (!customerInstance.save(flush: true)) {
            flash.message = message(code: 'default.not.created.message', args: [message(code: 'customer.label', default: 'Listing'), params.emailAddress])
            render(view: "create", model: [customerInstance: customerInstance])
            return
        }

        flash.message = message(code: 'default.created.message', args: [message(code: 'customer.label', default: 'Listing'), customerInstance.id])
        redirect(action: "show", id: customerInstance.id)
    }

    def edit() {
        def customerInstance = Customer.get(params.id)
        if (!customerInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'customer.label', default: 'Customer'), params.id])
            redirect(action: "list")
            return
        }

        [customerInstance: customerInstance]
    }

    def update() {
        def customerInstance = Customer.get(params.id)
        if (!customerInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'customer.label', default: 'Listing'), params.id])
            redirect(action: "list")
            return
        }

        if (params.version) {
            def version = params.version.toLong()
            if (customerInstance.version > version) {
                customerInstance.errors.rejectValue("version", "default.optimistic.locking.failure",
                        [message(code: 'customer.label', default: 'Listing')] as Object[],
                        "Another user has updated this Customer while you were editing")
                render(view: "edit", model: [listingInstance: listingInstance])
                return
            }
        }

        customerInstance.properties = params

        if (!customerInstance.save(flush: true)) {
            render(view: "edit", model: [customerInstance: customerInstance])
            return
        }

        flash.message = message(code: 'default.updated.message', args: [message(code: 'customer.label', default: 'Customer'), customerInstance.id])
        redirect(action: "show", id: customerInstance.id)
    }
}
